package com.ruoyi.project.loan.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.loan.domain.LoanProduct;
import com.ruoyi.project.loan.service.ILoanProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 贷款产品Controller
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RestController
@RequestMapping("/loan/product" )
public class LoanProductController extends BaseController {
    @Autowired
    private ILoanProductService loanProductService;

    /**
     * 查询贷款产品列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('loan:product:list')" )
    @GetMapping("/all/list" )
    public AjaxResult allList(LoanProduct loanProduct) {
        List<LoanProduct> list = loanProductService.selectLoanProductList(loanProduct);
        return success(list);
    }

    /**
     * 查询贷款产品列表
     */
    @PreAuthorize("@ss.hasPermi('loan:product:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(LoanProduct loanProduct) {
        startPage();
        List<LoanProduct> list = loanProductService.selectLoanProductList(loanProduct);
        return getDataTable(list);
    }

    /**
     * 导出贷款产品列表
     */
    @PreAuthorize("@ss.hasPermi('loan:product:export')" )
    @Log(title = "贷款产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, LoanProduct loanProduct) {
        List<LoanProduct> list = loanProductService.selectLoanProductList(loanProduct);
        ExcelUtil<LoanProduct> util = new ExcelUtil<LoanProduct>(LoanProduct.class);
        util.exportExcel(response, list, "贷款产品数据" );
    }

    /**
     * 获取贷款产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('loan:product:query')" )
    @GetMapping(value = "/{productId}" )
    public AjaxResult getInfo(@PathVariable("productId" ) Long productId) {
        return success(loanProductService.selectLoanProductByProductId(productId));
    }

    /**
     * 新增贷款产品
     */
    @PreAuthorize("@ss.hasPermi('loan:product:add')" )
    @Log(title = "贷款产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanProduct loanProduct) {
        return toAjax(loanProductService.insertLoanProduct(loanProduct));
    }

    /**
     * 修改贷款产品
     */
    @PreAuthorize("@ss.hasPermi('loan:product:edit')" )
    @Log(title = "贷款产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanProduct loanProduct) {
        return toAjax(loanProductService.updateLoanProduct(loanProduct));
    }

    /**
     * 删除贷款产品
     */
    @PreAuthorize("@ss.hasPermi('loan:product:remove')" )
    @Log(title = "贷款产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}" )
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(loanProductService.deleteLoanProductByProductIds(productIds));
    }
}
